﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<String> mails = new List<string>();
        List<List<String>> variosMails = new List<List<string>>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName);
                String temp = sr.ReadToEnd();
                sr.Close();
                String[] sep = {", "};
               List<String> lis = temp.Split(sep, StringSplitOptions.RemoveEmptyEntries).ToList();

               foreach (string s in lis)
               {
                   mails.Add(s);
               }

               
                int j = 1;
                bool b = true;
                while (b)
                {
                    List<String> a = new List<string>();
                    for (int i = 0; i < 50; i++)
                    {
                        try{
                            a.Add(mails[i * j]);
                        }
                        catch(Exception)
                        {
                            b = false;
                            break;
                        }
                    }
                    variosMails.Add(a);
                    j++;
                }
                int h = 0;
                if (!Directory.Exists("C:\\mails")) Directory.CreateDirectory("C:\\mails");

                foreach (List<String> list in variosMails)
                {
                    string aGuardar = list[0];

                    if (list.Count > 0)
                    {
                        for (int i = 1; i < list.Count; i++)
                        {
                            aGuardar += ", " + list[i];

                        }

                    }

                    StreamWriter sw = new StreamWriter("C:\\mails\\mails_" + h+".txt");
                    sw.WriteLine(aGuardar);
                    sw.Close();
                    h++;
                }

            }
        }
    }
}
